package cn.jwcat.controller;

import cn.jwcat.dealEntity.Bool;
import cn.jwcat.dealEntity.RoomDto;
import cn.jwcat.entity.Room;
import cn.jwcat.entity.Staff;
import cn.jwcat.service.IRoomService;
import cn.jwcat.utils.JwtTokenUtil;
import cn.jwcat.utils.result.Result;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 房屋操作
 */
@RestController
@Transactional
@RequestMapping("/room")
public class RoomController {
    @Resource
    IRoomService roomService;

    @Resource
    JwtTokenUtil jwtTokenUtil;

    @GetMapping("/{page}/{size}")
    @ApiOperation("分页查询获取病房列表")
    public Result getList(HttpServletRequest request, @PathVariable("page") int page, @PathVariable("size") int size) {
        Staff staff = jwtTokenUtil.getStaffFromRequest(request);
        PageHelper.startPage(page, size);
        List<RoomDto> rooms = roomService.getList(staff.getStaffId());
        PageInfo<RoomDto> roomPageInfo = new PageInfo<>(rooms);
        return Result.success(roomPageInfo);
    }

    @PostMapping("/status")
    @ApiOperation("修改病房启用状态")
    public Result changeStatus(@RequestBody Bool bool) {
        LambdaUpdateWrapper<Room> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Room::getStatus, bool.getBool()).eq(Room::getRoomId, bool.getId());
        boolean update = roomService.update(updateWrapper);
        if (update) return Result.success(bool.getName() + "号房间状态改变成功");
        return Result.code(401).message(bool.getName() + "号房间状态改变失败");
    }

    @PostMapping("/disinfection")
    @ApiOperation("修改病房消毒状态")
    public Result changeDisinfection(@RequestBody Bool bool) {
        LambdaUpdateWrapper<Room> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Room::getDisinfection, bool.getBool()).eq(Room::getRoomId, bool.getId());
        boolean update = roomService.update(updateWrapper);
        if (update) return Result.success(bool.getName() + "号房间消毒状态修改成功");
        return Result.code(401).message(bool.getName() + "号房间消毒状态修改失败");
    }

    @GetMapping("/{page}/{size}/{name}")
    @ApiOperation("根据病房姓名获取房间信息")
    public Result getListByName(HttpServletRequest request, @PathVariable("page") int page, @PathVariable("size") int size, @PathVariable("name") String name) {
        Staff staff = jwtTokenUtil.getStaffFromRequest(request);
        PageHelper.startPage(page, size);
        List<RoomDto> room = roomService.getListByName(staff.getStaffId(), name);
        PageInfo<RoomDto> roomPageInfo = new PageInfo<>(room);
        return Result.success(roomPageInfo);
    }

    @PutMapping
    @ApiOperation("更新病房信息")
    public Result putRoomRemark(@RequestBody RoomRemark roomRemark) {
        LambdaUpdateWrapper<Room> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Room::getRoomRemark, roomRemark.getRoomRemark()).eq(Room::getRoomId, roomRemark.getRoomId());
        boolean save = roomService.update(wrapper);
        if (save) return Result.success("备注修改成功");
        return Result.error().message("备注修改失败");
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class RoomRemark {
        private Integer roomId;
        private String roomRemark;
    }
}
